package com.my.mvc.db;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * JDBC的操作的工具类
 * Created by YJH on 2017/6/26 20:56.
 */
public class JdbcUtils {

    private static DataSource dataSource = null;

    static {
        //数据源只能被创建一次
        dataSource = new ComboPooledDataSource("mvcapp");
    }

    /**
     * 释放Connection连接
     *
     * @param connection Connection
     */
    public static void releaseConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 返回数据源的一个Connection对象
     *
     * @return Connection
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

}
